package com.example.demo.controllers;

import com.example.demo.common.utils.ToolsUtil;
import com.example.demo.entity.HD;
import com.example.demo.entity.KH;
import com.example.demo.services.HDService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: Ding XiJie
 */
@RequestMapping("/hd")
@Controller
public class HDController {
    @Autowired
    private HDService hdService;

    /**
     * @Author: Ding XiJie
     * @Description: 添加货单
     */
    @RequestMapping("/save")
    @ResponseBody
    public Map<String, Object> save(HD hd, HttpServletRequest request){
        try {
            Object userAttr = request.getSession().getAttribute("user");
            if (StringUtils.isEmpty(userAttr)){
                return ToolsUtil.returnMessage(0,"请先登录再操作");
            }
            KH kh = (KH) userAttr;
            hd.setKid(kh.getKid());
            hd = hdService.save(hd);
            return ToolsUtil.returnMessage(0,"添加成功");
        }catch (Exception e){
            e.printStackTrace();
            return ToolsUtil.returnMessage(0,"添加失败");
        }
    }

    /**
     * @Author: Ding XiJie
     * @Description: 修改货单
     */
    @RequestMapping("/update")
    @ResponseBody
    public Map<String, Object> update(HD hd){
        try {
            hd = hdService.update(hd);
            return ToolsUtil.returnMessage(0,"修改成功");
        }catch (Exception e){
            e.printStackTrace();
            return ToolsUtil.returnMessage(0,"修改失败");
        }
    }

    /**
     * @Author: Ding XiJie
     * @Description: 删除货单
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Map<String, Object> delete(Integer hdid){
        try {
            hdService.delete(hdid);
            return ToolsUtil.returnMessage(0,"删除成功");
        }catch (Exception e){
            e.printStackTrace();
            return ToolsUtil.returnMessage(0,"删除失败");
        }
    }

    /**
     * @Author: Ding XiJie
     * @Description: 查询所有快递/货物
     */
    @RequestMapping("/getAllHd")
    @ResponseBody
    public Map<String, Object> getAllHd(){
        Integer count = 0;
        try {
            List<HD> khList = hdService.getAllHd();
            count = hdService.findCount();
            return ToolsUtil.returnTable(0,count,"查询成功",khList);
        }catch (Exception e){
            e.printStackTrace();
            return ToolsUtil.returnTable(0,count,"查询失败",null);
        }
    }


    /**
     * @Author: Ding XiJie
     * @Description: 根据用户kid查询所有快递/货物
     */
    @RequestMapping("/getAllHdByKid")
    @ResponseBody
    public Map<String, Object> getAllHdByKid(Integer kid){
        Integer count = 0;
        try {
            List<HD> khList = hdService.getAllHdByKid(kid);
            count = hdService.findCountByKid(kid);
            return ToolsUtil.returnTable(0,count,"查询成功",khList);
        }catch (Exception e){
            e.printStackTrace();
            return ToolsUtil.returnTable(0,count,"查询失败",null);
        }
    }

    /**
     * @Author: Ding XiJie
     * @Description: 根据用户kid和hdno查询所有快递/货物
     */
    @RequestMapping("/getAllHdByHdnoAndKno")
    @ResponseBody
    public Map<String, Object> getAllHdByHdnoAndKno(Integer kid,Integer hdno){
        Integer count = 0;
        try {
            List<HD> khList = hdService.getAllHdByHdnoAndKno(kid,hdno);
            count = hdService.findCountByKidAndHdno(kid,hdno);
            return ToolsUtil.returnTable(0,count,"查询成功",khList);
        }catch (Exception e){
            e.printStackTrace();
            return ToolsUtil.returnTable(0,count,"查询失败",null);
        }
    }


    /**
     * @Author: Ding XiJie
     * @Description: 跳转页面
     */
    @RequestMapping("/toKdgl")
    public String toKdgl(Model model){
        return "kdgl.html";
    }

    /**
     * @Author: Ding XiJie
     * @Description: 跳转页面
     */
    @RequestMapping("/toKdglWithData")
    @ResponseBody
    public Object toKdglWithData(Model model){
        List<Map> mapList = new ArrayList<>();
        return ToolsUtil.returnTable(0,10,"查询成功",mapList);
    }

    /**
     * @Author: Ding XiJie
     * @Description: 跳转页面
     */
    @RequestMapping("/toHdList")
    public String toHdList(Model model){
        return "kdgl_hd.html";
    }

    /**
     * @Author: Ding XiJie
     * @Description: 跳转页面
     */
    @RequestMapping("/toUserHdList")
    public String toUserHdList(){
        return "user_hd.html";
    }
}
